define(function(require, exports){
	var $       = require("jquery");
	var util    = require('util');
	var pubjs   = require('pubjs');
	var view    = require('@base/view');

	var Main = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-brandMain',
				'vModel':{
					'sum': null, // 总金额
					'balance': null, // 充值余额
					'availableCredit': null, // 可用额度
					'isOne': true  //单日投放效果状态
				}
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();

			var finance = [
				{'name': LANG('总金额'), 'type': 'sum', 'unit': LANG('元')},
				{'name': LANG('充值余额'), 'type': 'balance', 'unit': LANG('元')},
				{'name': LANG('可用额度'), 'type': 'availableCredit', 'unit': LANG('元')}
			];
			var shortcut = [
				{'name': LANG('广告主'), 'icon': 'icon-user', 'href': '#admin/aptitude/edit'},
				{'name': LANG('计划'), 'icon': 'icon-plan', 'href': '#serving/planEdit'},
				{'name': LANG('推广组'), 'icon': 'icon-campaign', 'href': '#serving/campaignEdit'},
				{'name': LANG('创意包'), 'icon': 'icon-sweety', 'href': '#serving/sweetyEdit'},
				{'name': LANG('落地页'), 'icon': 'icon-whisky', 'href': '#serving/whiskyEdit'}
			];

			self.append($([
				'<div class="M-formHead">',
				'</div>',
				'<div class="row-fluid">',
					'<div class="col7 ex-grid panel panel-default financePanel">',
						'<div class="panel-title">',
							'<span class="title">' + LANG('账号余额') + '</span>',
							'<span class="fr dn">',
								'<span pub-name="recharge" pub-mod="@base/common/input.button"></span>',
								'<span pub-name="account" pub-mod="@base/common/input.button"></span>',
							'</span>',
						'</div>',
						'<div class="panel-body">',
							'<ul class="row-fluid"/>',
						'</div>',
					'</div>',
					'<div class="col5 ex-grid panel panel-default shortcutPanel">',
						'<div class="panel-title">',
							'<span class="title">' + LANG('快捷新建') + '</span>',
						'</div>',
						'<div class="panel-body">',
							'<ul class="row-fluid"/>',
						'</div>',
					'</div>',
				'</div>',
				'<div class="panel panel-default mt20 chartPanel">',
					'<div class="panel-title">',
						'<div class="title act" data-type="one">' + LANG('单日投放效果') + '</div>',
						'<div class="title" data-type="more">' + LANG('历史投放效果') + '</div>',
					'</div>',
					'<div class="panel-body">',
						'<div v-show = "isOne" pub-name = "comparison" pub-mod="modules/chart.comparison"></div>',
						'<div v-show = "!isOne" pub-name = "summary" pub-mod="modules/chart.summary"></div>',
					'</div>',
				'</div>'
			].join('')));

			// 构建账号余额
			util.each(finance, function(item, idx){
				if(item){
					$([
						'<li class="">',
							'<div class="name">'+item.name+'</div>',
							'<span class="number" v-html="'+item.type+'">' + '</span>',
							// '<span class="unit">'+item.unit+'</span>',
						'</li>'
					].join('')).appendTo(el.find('.financePanel .panel-body ul')).width(100/finance.length+'%');
				}
			});

			// 构建快捷新建
			util.each(shortcut, function(item, idx){
				if(item){
					$([
						'<li title="'+item.name+'" class="">',
							'<a class="dib" href="'+item.href+'" target="_blank">',
								'<i class="font-primary '+item.icon+'"/>',
								'<span>'+item.name+'</span>',
							'</a>',
						'</li>'
					].join('')).appendTo(el.find('.shortcutPanel .panel-body ul')).width(100/shortcut.length+'%');
				}
			});

			self.uiBind(el.find('.chartPanel .panel-title .title'), 'click', 'eventChargeChart');

			this.vm.mount();

			var config = {
				// 充值
				'recharge': {
					'value': LANG('充值'),
					'class': 'btn btn-primary mr10',
					'width': null
				},
				// 帐户明细
				'account': {
					'value': LANG('帐户明细'),
					'class': 'btn btn-default',
					'width': null
				}
			};


			self.buildFromTemplate(config);
		},
		afterBuildTemplate: function(){
			this.load();
		},
		onInputClick: function(ev){
			switch (ev.name){
				case 'recharge':
					break;
				case 'account':
					break;
			}
			return false;
		},
		eventChargeChart: function(evt, elm){
			$(elm).siblings().removeClass('act');
			$(elm).addClass('act');

			var type = $(elm).attr('data-type');
			this.vm.$.isOne = (type == 'one');
			this.load();
		},
		// 格式化余额
		formatRest:function(val){
			val = util.round0(val,2);
			val = pubjs.currRate(val);
			val = String(val).split('.');
			var html = [
				'<span>'+val[0]+'</span>',
				'<span>.'+(val[1] || '00')+'</span>',
				'<span class="pl5">'+pubjs.currUnit()+'</span>',
			].join('');
			return html;
		},
		load: function(){
			pubjs.mc.send('cost/info', this.onload.bind(this));
			if(this.vm.$.isOne){
				if(this.$.comparison){
					this.$.comparison.load();
				}
			}
			else{
				if(this.$.summary){
					this.$.summary.load();
				}

			}
			return this;
		},
		reload: function(){
			this.load();
			return this;
		},
		onload: function(err, data){
			if (err){
				pubjs.log(err.message);
				pubjs.alert(err.message);
				return false;
			}
			this.setData({
				'rest': data.Remain,
				'used': data.TodayCost
			});
		},
		setData:function(data){
			if(data){
				this.vm.$.balance = this.formatRest(data.rest);
				this.vm.$.used = this.formatRest(data.used);
				// 可用额度计算公式,
				// 在余额小于0的情况下：可用额度+账号余额，如果小0即等于0，否则等于差值
				var current = pubjs.getUser().current;
				var allowAdCredit = 0;
				if (current) {
					allowAdCredit = current.Finance.Credit.AllowAdCredit;
				}
				if(data.rest < 0){
					allowAdCredit += data.rest;
					allowAdCredit = allowAdCredit < 0 ? 0 : allowAdCredit;
				}
				this.vm.$.availableCredit = this.formatRest(allowAdCredit);
				var sum = allowAdCredit + data.rest;
				this.vm.$.sum = this.formatRest(sum);
			}
		},
	});
	exports.main = Main;
});